/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbm.smsbroadcast;

import com.elcom.emocbc.util.ConfigurationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
public class SmsBroadcastConfig {

    private final static Logger logger = LoggerFactory.getLogger(SmsBroadcastConfig.class);

    public static boolean smsBroadcastEnable() {
        ConfigurationManager.reloadConfiguration();
        boolean smsBroadcastEnable = ConfigurationManager.getBooleanProp("cell_broadcast", "SmsBroadcast.global.enable", false);
        if (smsBroadcastEnable) {
            logger.debug("[SmsBroadcast] sms broadcast is enabled");
            double broadcastRate = ConfigurationManager.getDoubleProp("cell_broadcast", "SmsBroadcast.global.broadcastRate", 1);
            double random = Math.random();
            logger.debug("[SmsBroadcast] Broadcast rate is {}, ramdomized {}", broadcastRate, random);
            return random <= broadcastRate;
        } else {
            return false;
        }
    }

    public static boolean smsBroadcastEnable(String msisdn) {
        ConfigurationManager.reloadConfiguration();
        boolean smsBroadcastEnable = ConfigurationManager.getBooleanProp("cell_broadcast", "SmsBroadcast." + msisdn + ".enable", true);
        if (smsBroadcastEnable) {
            logger.debug("[SmsBroadcast] sms broadcast to {} is enabled", msisdn);
            double broadcastRate = ConfigurationManager.getDoubleProp("cell_broadcast", "SmsBroadcast." + msisdn + ".broadcastRate", 1);
            double random = Math.random();
            logger.debug("[SmsBroadcast] {} Broadcast rate is {}, ramdomized {}", msisdn, broadcastRate, random);
            return random <= broadcastRate;
        } else {
            return false;
        }
    }
}
